home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gnat1792.zip / gnat179b / t-adainc / a-strunb.adb < prev    next >
Text File  |  1994-05-19  |  15KB  |  559 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                A D A . S T R I N G S . U N B O U N D E D                 --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.5 $                              --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. --
  22. --                                                                          --
  23. ------------------------------------------------------------------------------
  24.  
  25. --  Note: This code is derived from the ADAR.CSH public domain Ada 83
  26. --  versions of the Appendix C string handling packages.
  27.  
  28. with Ada.Strings.Fixed;
  29. with Ada.Strings.Search;
  30. with Ada.Unchecked_Deallocation;
  31.  
  32. package body Ada.Strings.Unbounded is
  33.  
  34.    -----------------------
  35.    -- Local Subprograms --
  36.    -----------------------
  37.  
  38.    procedure Free (Handle : in out Unbounded_String);
  39.    --  Free an unbounded string using unchecked deallocation. This is used
  40.    --  only internally to this package by those routines which must be sure
  41.    --  to free a string before reassigning it. This is a temporary kludge
  42.    --  to make up for the fact that we do not have finalization yet! ???
  43.  
  44.    ---------
  45.    -- "=" --
  46.    ---------
  47.  
  48.    function "="  (Left, Right : Unbounded_String) return Boolean is
  49.    begin
  50.       return Left.Reference.all = Right.Reference.all;
  51.    end "=";
  52.  
  53.    ---------
  54.    -- "<" --
  55.    ---------
  56.  
  57.    function "<"  (Left, Right : Unbounded_String) return Boolean is
  58.    begin
  59.       return Left.Reference.all < Right.Reference.all;
  60.    end "<";
  61.  
  62.    ----------
  63.    -- "<=" --
  64.    ----------
  65.  
  66.    function "<=" (Left, Right : Unbounded_String) return Boolean is
  67.    begin
  68.       return Left.Reference.all <= Right.Reference.all;
  69.    end "<=";
  70.  
  71.    ---------
  72.    -- ">" --
  73.    ---------
  74.  
  75.    function ">"  (Left, Right : Unbounded_String) return Boolean is
  76.    begin
  77.       return Left.Reference.all > Right.Reference.all;
  78.    end ">";
  79.  
  80.    ----------
  81.    -- ">=" --
  82.    ----------
  83.  
  84.    function ">=" (Left, Right : Unbounded_String) return Boolean is
  85.    begin
  86.       return Left.Reference.all >= Right.Reference.all;
  87.    end ">=";
  88.  
  89.    ---------
  90.    -- "*" --
  91.    ---------
  92.  
  93.    function "*"
  94.      (Left  : Natural;
  95.       Right : Character)
  96.       return  Unbounded_String
  97.    is
  98.       Result : Unbounded_String := (Reference => new String (1 .. Left));
  99.  
  100.    begin
  101.       Result.Reference.all := (1 .. Left => Right);
  102.       return Result;
  103.    end "*";
  104.  
  105.    function "*"
  106.      (Left  : Natural;
  107.       Right : String)
  108.      return   Unbounded_String
  109.    is
  110.       Result : Unbounded_String :=
  111.          (Reference => new String (1 .. Left * Right'Length));
  112.  
  113.    begin
  114.       for J in 1 .. Left loop
  115.          Result.Reference.all
  116.            (Right'Length * J - Right'Length + 1 .. Right'Length * J) := Right;
  117.       end loop;
  118.  
  119.       return Result;
  120.    end "*";
  121.  
  122.    function "*"
  123.      (Left  : Natural;
  124.       Right : Unbounded_String)
  125.       return  Unbounded_String
  126.    is
  127.       R_Length : constant Integer := Right.Reference.all'Length;
  128.       Result   : Unbounded_String :=
  129.         (Reference => new String (1 .. Left * Right.Reference.all'Length));
  130.  
  131.    begin
  132.       for I in 1 .. Left loop
  133.          Result.Reference.all (R_Length * I - R_Length + 1 .. R_Length * I) :=
  134.            Right.Reference.all;
  135.       end loop;
  136.  
  137.       return Result;
  138.    end "*";
  139.  
  140.    ---------
  141.    -- "&" --
  142.    ---------
  143.  
  144.    function "&"
  145.     (Left, Right : Unbounded_String)
  146.      return        Unbounded_String
  147.    is
  148.       L_Length : constant Integer := Left.Reference.all'Length;
  149.       R_Length : constant Integer := Right.Reference.all'Length;
  150.       Length   : constant Integer :=  L_Length + R_Length;
  151.       Result   : Unbounded_String := (Reference => new String (1 .. Length));
  152.  
  153.    begin
  154.       Result.Reference.all (1 .. L_Length)          := Left.Reference.all;
  155.       Result.Reference.all (L_Length + 1 .. Length) := Right.Reference.all;
  156.       return Result;
  157.    end "&";
  158.  
  159.    function "&"
  160.      (Left  : Unbounded_String;
  161.       Right : String)
  162.       return  Unbounded_String
  163.    is
  164.       L_Length : constant Integer := Left.Reference.all'Length;
  165.       Length   : constant Integer := L_Length +  Right'Length;
  166.       Result   : Unbounded_String := (Reference => new String (1 .. Length));
  167.    begin
  168.       Result.Reference.all (1 .. L_Length)          := Left.Reference.all;
  169.       Result.Reference.all (L_Length + 1 .. Length) := Right;
  170.       return Result;
  171.    end "&";
  172.  
  173.    function "&"
  174.      (Left  : String;
  175.       Right : Unbounded_String)
  176.       return  Unbounded_String
  177.    is
  178.       R_Length : constant Integer := Right.Reference.all'Length;
  179.       Length   : constant Integer := Left'Length + R_Length;
  180.       Result   : Unbounded_String := (Reference => new String (1 .. Length));
  181.  
  182.    begin
  183.       Result.Reference.all (1 .. Left'Length)          := Left;
  184.       Result.Reference.all (Left'Length + 1 .. Length) := Right.Reference.all;
  185.       return Result;
  186.    end "&";
  187.  
  188.    function "&"
  189.      (Left  : Unbounded_String;
  190.       Right : Character)
  191.       return  Unbounded_String
  192.    is
  193.       Length : constant Integer := Left.Reference.all'Length + 1;
  194.       Result : Unbounded_String := (Reference => new String (1 .. Length));
  195.  
  196.    begin
  197.       Result.Reference.all (1 .. Length - 1) := Left.Reference.all;
  198.       Result.Reference.all (Length)          := Right;
  199.       return Result;
  200.    end "&";
  201.  
  202.    function "&"
  203.      (Left  : Character;
  204.       Right : Unbounded_String)
  205.       return  Unbounded_String
  206.    is
  207.       Length : constant Integer := Right.Reference.all'Length + 1;
  208.       Result : Unbounded_String := (Reference => new String (1 .. Length));
  209.  
  210.    begin
  211.       Result.Reference.all (1)           := Left;
  212.       Result.Reference.all (2 .. Length) := Right.Reference.all;
  213.       return Result;
  214.    end "&";
  215.  
  216.    -----------
  217.    -- Count --
  218.    -----------
  219.  
  220.    function Count
  221.      (Source   : Unbounded_String;
  222.       Pattern  : String;
  223.       Mapping  : Maps.Character_Mapping := Maps.Identity)
  224.       return     Natural
  225.    is
  226.    begin
  227.       return Search.Count (Source.Reference.all, Pattern, Mapping);
  228.    end Count;
  229.  
  230.    function Count
  231.      (Source   : Unbounded_String;
  232.       Set      : Maps.Character_Set)
  233.       return     Natural
  234.    is
  235.    begin
  236.       return Search.Count (Source.Reference.all, Set);
  237.    end Count;
  238.  
  239.    ------------
  240.    -- Create --
  241.    ------------
  242.  
  243.    procedure Create
  244.      (Target : in out Unbounded_String;
  245.       Length : Natural)
  246.    is
  247.    begin
  248.       Free (Target);
  249.       Target := (Reference => new String (1 .. Length));
  250.    end Create;
  251.  
  252.    ------------
  253.    -- Delete --
  254.    ------------
  255.  
  256.    function Delete
  257.      (Source  : Unbounded_String;
  258.       From    : Positive;
  259.       Through : Natural)
  260.       return    Unbounded_String
  261.    is
  262.    begin
  263.       return
  264.         To_Unbounded_String
  265.           (Fixed.Delete (Source.Reference.all, From, Through));
  266.    end Delete;
  267.  
  268.    -------------
  269.    -- Element --
  270.    -------------
  271.  
  272.    function Element
  273.